package shajie.shajun.mmhf.Adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.media.MediaScannerConnection;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import Util.ImageLoader;
import Util.Util;
import shajie.shajun.mmhf.R;
import shajie.shajun.mmhf.View.Fragment.BigPicture_Fragment;


/**
 * Created by admin on 2018/1/22.
 */

public class GridViewAdapter extends BaseAdapter {
    public LayoutInflater inflater;
    public String[] imageUrls;
    private Context mContext;
    private boolean unload=false;
    private int layout;
    FragmentManager mFragmentManager;
    public GridViewAdapter(Context context, String[] imageUrls, boolean unload, int layout, FragmentManager fragmentManager) {
        inflater = LayoutInflater.from(context);
        this.imageUrls = imageUrls;
        this.mContext=context;
        this.unload=unload;
        this.layout=layout;
        this.mFragmentManager=fragmentManager;
    }
    @Override
    public int getCount() {
        return imageUrls.length;
    }

    @Override
    public String getItem(int position) {
        return imageUrls[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        String currStr = getItem(position);
        View view;
        ViewHolder holder;
        if(convertView == null)
        {
            view = inflater.inflate(this.layout, null);
            holder = new ViewHolder();
            holder.imageView = (ImageView) view.findViewById(R.id.img_product);
            holder.mTextView = (TextView) view.findViewById(R.id.tv_picturename);
            view.setTag(holder);
        }else
        {
            view = convertView;
            holder = (ViewHolder) view.getTag();
        }
      //  holder.imageView.setTag(currStr);
        ImageLoader.load(mContext,holder.imageView,imageUrls[position]);
        holder.mTextView.setText(imageUrls[position].substring(imageUrls[position].lastIndexOf("/")+1,imageUrls[position].length()));
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (unload) {
                    new AlertDialog.Builder(mContext) // build AlertDialog
                            .setTitle("选择操作") // title
                            .setItems(R.array.selectpictures, new  DialogInterface.OnClickListener() { //content
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    switch (which){
                                        case 0:
                                            String url=imageUrls[position];
                                            String camerapath=Environment.getExternalStorageDirectory().getAbsolutePath()+"/DCIM/Camera/IMG_"+url.substring(url.lastIndexOf("/")+1,url.length());

                                            Util.updatafile(url, camerapath, new Interface.MCallBack() {
                                                @Override
                                                public void succes(String s) {
                                                    Message msg = myHandler.obtainMessage();
                                                    msg.what = 0; //消息标识
                                                    msg.obj = s;
                                                    myHandler.sendMessage(msg); //发送消息
                                                }
                                            });
                                            break;
                                        case 1:
                                        openBigPicture(imageUrls[position]);
                                        break;
                                    }

                                }
                            }) .show();


                }else {
                    openBigPicture(imageUrls[position]);
                }
            }
        });
        return view;
    }

private void openBigPicture(String url){
    mFragmentManager.beginTransaction()
            .addToBackStack(null)
            .setCustomAnimations(R.anim.in_from_left,R.anim.in_from_right,R.anim.out_from_left,R.anim.out_from_right)
            .add(R.id.container,BigPicture_Fragment.NewIntence(url))
            .commit();
}
    static class ViewHolder
    {
        ImageView imageView;
        TextView mTextView;
    }
    //主线程
    Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch(msg.what) {
                case 0:
                    Toast.makeText(mContext, "下载完成", Toast.LENGTH_SHORT).show();
                    /** MediaScanner 扫描更新图片图片 **/
                    MediaScannerConnection.scanFile(mContext, new String[] {msg.obj.toString()}, null, null);
                    break;
                default:
                    break;
            }
        }
    };

}
